const http = require('http');

// 测试Android应用需要的API端点
function testAndroidAPIs() {
    const baseUrl = 'http://172.16.0.39:3000';
    
    console.log('🧪 测试Android应用连接...\n');
    
    // 测试所有资源目录的API
    const directories = [
        'SolarPower',
        'VoiceInteractiveMap', 
        'TurbofanEngine',
        'Hannault',
        'KimAqQarav',
        'Lightnin_Glow',
        'Crooks_Radiometer',
        'Manipulator',
        'Remote_Piano',
        'Fun_Beginner_AI',
        'Specimen_Wheat',
        'Specimen_Soil',
        'Specimen_Silkworm',
        'Specimen_Dragonfly',
        'Specimens_FoodCrops',
        'Specimens_Arthropods',
        'specimen_beanSprouts'
    ];
    
    let completed = 0;
    let total = directories.length;
    
    directories.forEach((dir, index) => {
        setTimeout(() => {
            testDirectoryAPI(baseUrl, dir, () => {
                completed++;
                if (completed === total) {
                    console.log(`\n✅ 所有 ${total} 个资源目录API测试完成！`);
                    console.log('🎉 Android应用可以正常连接服务器！');
                }
            });
        }, index * 100); // 每100ms测试一个目录
    });
}

function testDirectoryAPI(baseUrl, directory, callback) {
    const options = {
        hostname: '172.16.0.39',
        port: 3000,
        path: `/static/api/directory/${directory}`,
        method: 'GET',
        headers: {
            'Accept': 'application/json'
        }
    };

    const req = http.request(options, (res) => {
        let data = '';
        res.on('data', (chunk) => {
            data += chunk;
        });
        
        res.on('end', () => {
            try {
                const jsonData = JSON.parse(data);
                if (res.statusCode === 200 && jsonData.files && jsonData.files.length > 0) {
                    console.log(`✅ ${directory}: ${jsonData.files.length} 个文件`);
                } else {
                    console.log(`❌ ${directory}: 无文件或错误`);
                }
            } catch (e) {
                console.log(`❌ ${directory}: JSON解析失败`);
            }
            callback();
        });
    });

    req.on('error', (e) => {
        console.log(`❌ ${directory}: 连接失败 - ${e.message}`);
        callback();
    });

    req.setTimeout(5000, () => {
        console.log(`⏰ ${directory}: 请求超时`);
        req.destroy();
        callback();
    });

    req.end();
}

// 测试静态资源访问
function testStaticResource() {
    console.log('\n🔍 测试静态资源访问...');
    
    const options = {
        hostname: '172.16.0.39',
        port: 3000,
        path: '/static/html/SolarPower/SolarPower.html',
        method: 'GET'
    };

    const req = http.request(options, (res) => {
        console.log(`📄 SolarPower.html: 状态码 ${res.statusCode}, 大小 ${res.headers['content-length']} 字节`);
        
        let data = '';
        res.on('data', (chunk) => {
            data += chunk;
        });
        
        res.on('end', () => {
            if (data.includes('<!DOCTYPE html>')) {
                console.log('✅ HTML文件内容正常');
            } else {
                console.log('❌ HTML文件内容异常');
            }
        });
    });

    req.on('error', (e) => {
        console.log(`❌ 静态资源访问失败: ${e.message}`);
    });

    req.end();
}

// 开始测试
testAndroidAPIs();

// 延迟测试静态资源
setTimeout(() => {
    testStaticResource();
}, 2000);
